package ace.module.oauth2.server.core.impl.converter.impl;

import ace.module.oauth2.server.core.impl.converter.ClientAuthenticationMethodToStringConverter;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.oauth2.core.ClientAuthenticationMethod;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/10/12 11:50
 */
@Component
public class ClientAuthenticationMethodToStringConverterImpl
    implements ClientAuthenticationMethodToStringConverter {

  @Override
  public String convert(ClientAuthenticationMethod source) {
    if (source == null) {
      return StringUtils.EMPTY;
    }
    return source.getValue();
  }

  @Override
  public Set<String> convert(Set<ClientAuthenticationMethod> sources) {
    if (CollectionUtils.isEmpty(sources)) {
      return new HashSet<>();
    }
    return sources.stream().map(p -> this.convert(p)).collect(Collectors.toSet());
  }

  @Override
  public String convertToString(Set<ClientAuthenticationMethod> sources) {
    if (CollectionUtils.isEmpty(sources)) {
      return StringUtils.EMPTY;
    }
    return sources.stream().map(p -> this.convert(p)).collect(Collectors.joining(","));
  }
}
